<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">


      <el-form-item label="城市等级">
        <el-select v-model="queryParams2.cityRank" placeholder="请选择"  @input=getList>
          <el-option
            v-for="item in cityRankList2"
            :key="item"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="城市">
        <el-select v-model="registerCity" multiple placeholder="请选择" @input=getList>
          <el-option
            v-for="item in cityRankList3"
            :key="item"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="行政区">
        <el-select v-model="registerArea" multiple placeholder="请选择" @input=getList>
          <el-option
            v-for="item in cityRankList4"
            :key="item"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>

      <br>






      <el-form-item label="注册时间" prop="registDate">
        <div class="block">

          <el-date-picker
            v-model="registDate"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          value-format="yyyy-MM-dd"
          @input="getList">
          </el-date-picker>

        </div>
      </el-form-item>

      <el-form-item label="用户类型" prop="customerType">
        <el-checkbox-group min="1" v-model="customerType" @input="getList">
          <el-checkbox-button v-for="city in cityOptions" :label="city.value" :key="city.value">
            {{ city.label }}
          </el-checkbox-button>
        </el-checkbox-group>
      </el-form-item>


      <el-form-item label="" prop="realName">
        <el-input
          v-model="queryParams.abc"
          placeholder="输入用户名、手机号查询"
          clearable
          @keyup.enter.native="handleQuery"
          suffix-icon="el-icon-search"
          @input="getList"
        />
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:customer:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column label="用户序号" align="center" prop="id" fixed />
      <el-table-column label="用户ID" align="center" prop="uid" fixed />
      <el-table-column label="用户名称" align="center" prop="realName" fixed />
      <el-table-column label="用户手机号" align="center" prop="phone" />
      <el-table-column label="用户类型" align="center" prop="customerType" width="180">
        <template slot-scope="scope">
        <span v-if="scope.row.customerType === 1 ">
            <el-tag >乘客</el-tag>
        </span>
          <span v-else>
           <el-tag >乘客</el-tag><br>
           <el-tag type="warning" style="margin-top: 5px">司机</el-tag>
        </span>
        </template>
      </el-table-column>
      <el-table-column label="用户性别" align="center" prop="gender" >
        <template v-slot="scope">
          <span v-if="scope.row.gender === 1 ">男</span>
          <span v-else>女</span>
        </template>
      </el-table-column>
      <el-table-column label="生日" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="注册时所在城市" align="center" prop="registerCity" />
      <el-table-column label="注册时所在行政区" align="center" prop="registerArea" />
      <el-table-column label="最近一次登录时间" align="center" prop="lastLogin" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastLogin, '{y}-{m}-{d} ') }}</span><br/>
          <span>{{ parseTime(scope.row.lastLogin, '{h}:{m} ') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" prop="registDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registDate, '{y}-{m}-{d} ') }}</span><br/>
          <span>{{ parseTime(scope.row.registDate, '{h}:{m} ') }}</span>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { listCustomer} from "@/api/basicFeatures/userManagement/userProfile/userprofile";
import { listCityRank} from "@/api/cityRank/cityRank";
const a= ["一线城市","二线城市","三线城市"];
export default {
  name: "Customer",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户信息表格数据
      customerList: [],
      // 接收城市等级表格数据
      cityRankList:[],
      //城市等级数据 一线城市 二线城市 三线城市
      cityRankList2:a,
      //城市数据 去重 展示
      cityRankList3:[],
      //行政区数据 去重 展示
      cityRankList4:[],
      //城市数据  数据绑定
      registerCity:[],
      //县级行政区数据 数据绑定
      registerArea:[],
      //用户类型数据 数据绑定
      customerType: [1,2],
      //用户类型数据
      cityOptions: [
        { label: '乘客', value: 1 },
        { label: '司机', value: 2 }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 用户信息表 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        abc:'',
        customerType: null,
        startDate:'',
        endDate:'',
        registerCity:'',
        registerArea:'',
      },
      //注册时间数据
      registDate:'',
      //一框多查 用户姓名、用户手机号
      abc:'',
      // // 城市等级表    查询参数
      queryParams2: {
        pageNum: 1,
        pageSize: 1000,
        cityRank:'',
        city:''
      },


      // 表单校验
      rules: {
        uid: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        realName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ],
        customerType: [
          { required: true, message: "用户类型不能为空", trigger: "change" }
        ],
        registDate: [
          { required: true, message: "注册时间不能为空", trigger: "blur" }
        ]
      },

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户信息列表 */
    getList() {
      this.loading = true;
     // 调用城市接口
      listCityRank(this.queryParams2).then(response => {
        //如果接口调用正确
        if (response.code==200){
          //接收返回响应
          this.cityRankList = response.rows;
          //接收返回响应中的城市数据 去重
          this.cityRankList3=Array.from(new Set(this.cityRankList.map(item => item.city.trim())));
          //接收返回响应中的县级行政区数据 去重
          this.cityRankList4=Array.from(new Set(this.cityRankList.map(item => item.administrative_districts.trim())));
          //判断查询参数是否为空
          if(this.queryParams2.cityRank!=''){
            //将this.cityRankList中的city转成字符串赋值给this.queryParams.registerCity
            this.queryParams.registerCity = Array.from(new Set(this.cityRankList.map(item => item.city.trim()))).join(',');
          }else{
            this.queryParams.registerCity='';
          }
          //判断注册城市是否有值
          if (this.registerCity.length === 0) {
          } else {
            this.queryParams.registerCity = Array.from(new Set(this.registerCity.map(item => item.trim()))).join(',');
            this.cityRankList4 = this.cityRankList
              .filter(item => this.registerCity.includes(item.city.trim()))
              .map(item => item.administrative_districts.trim());
            this.cityRankList4 = Array.from(new Set(this.cityRankList4));
          }
          //判断注册行政区是否有值
          if (this.registerArea.length === 0) {
            this.queryParams.registerArea='';
          } else {
            this.queryParams.registerArea = Array.from(new Set(this.registerArea.map(item => item.trim()))).join(',');
          }

          //判断注册时间
          if (this.registDate!=''){
            const a= this.registerCity.toString();
            const dates = a.split(',');
            this.queryParams.startDate = dates[0];
            this.queryParams.endDate = dates[1];
          }

          //判断用户类型
          if (this.customerType.length>1){
            this.queryParams.customerType = null;
          }else{
            this.queryParams.customerType = this.customerType[0];
          }

          //调用用户接口
          listCustomer(this.queryParams).then(response => {
            this.customerList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      });

    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.abc = '';
      this.registerCity=[];
      this.registerArea=[];
      this.customerType=[1,2];
      this.registDate='';
      this.queryParams.pageNum = 1;
      this.queryParams.abc = '';
      this.queryParams.customerType = null;
      this.queryParams.startDate = '';
      this.queryParams.endDate = '';
      this.queryParams.endDate = '';
      this.queryParams.registerCity='';
      this.queryParams.registerArea='';
      this.queryParams2.cityRank = '';
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('system/customer/export', {
        ...this.queryParams
      }, `customer_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>

.el-checkbox-button.is-disabled .el-checkbox-button__inner {
  color: white;
  cursor: not-allowed;
  background-image: none;
  background-color: #00afff;
  border-color: #00afff;
  -webkit-box-shadow: none;
  box-shadow: none;
}
</style>
